package com.viking.car;


import java.io.UnsupportedEncodingException;

public class Token {
    private String type;           //1位，1-一般预定订单token，2-一次性token
    private String orderNo;        //32位，订单编号，如: 000000000F2014031812000814648141
    private String userId;         //32位，用户唯一编号，如: 0907e7826f744542b492bbd3596439db
    private String carId;          //17位，车辆唯一编号,如: WBA3A5507CNN00000
    private String orderStartDt;   //14位，订单开始时间，如: 20140319083000 ,代表2014-03-19 08:30:00 订单开始
    private String orderStartDtOff;//4位，允许的订单开始时间向前偏移量，单位秒,如0900 ,代表订单开始时间可以向前推移900秒
    private String orderEndDt;     //14位，订单结束时间
    private String orderEndDtOff;  //4位，允许的订单结束时间向后偏移量，单位秒，如0900,代表订单结束始时间可以向后推移900秒
    public String Reserved;        //4位，保留字段，方便以后扩充,目前可用0x00 0x00 0x00 0x00 填充
    public Token(){
        byte[] temp = new byte[4];
        for (int i=0;i<4;i++){
            temp[i] = (byte)0x00;
        }
        try {
            Reserved = new String(temp,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getOrderNo() {
        return orderNo;
    }

    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getCarId() {
        return carId;
    }

    public void setCarId(String carId) {
        this.carId = carId;
    }

    public String getOrderStartDt() {
        return orderStartDt;
    }

    public void setOrderStartDt(String orderStartDt) {
        this.orderStartDt = orderStartDt;
    }

    public String getOrderStartDtOff() {
        return orderStartDtOff;
    }

    public void setOrderStartDtOff(String orderStartDtOff) {
        this.orderStartDtOff = orderStartDtOff;
    }

    public String getOrderEndDt() {
        return orderEndDt;
    }

    public void setOrderEndDt(String orderEndDt) {
        this.orderEndDt = orderEndDt;
    }

    public String getOrderEndDtOff() {
        return orderEndDtOff;
    }

    public void setOrderEndDtOff(String orderEndDtOff) {
        this.orderEndDtOff = orderEndDtOff;
    }

    public String getReserved() {
        return Reserved;
    }

    public void setReserved(String reserved) {
        Reserved = reserved;
    }

    @Override
    public String toString(){
        StringBuffer buffer = new StringBuffer();
        buffer.append(type).append(orderNo)
              .append(userId).append(carId)
              .append(orderStartDt).append(orderStartDtOff)
              .append(orderEndDt).append(orderEndDtOff)
              .append(Reserved);
        return buffer.toString();
    }

    public String toFormatString() {
        return "Token{" +
                "type='" + type + '\'' +
                ", orderNo='" + orderNo + '\'' +
                ", userId='" + userId + '\'' +
                ", carId='" + carId + '\'' +
                ", orderStartDt='" + orderStartDt + '\'' +
                ", orderStartDtOff='" + orderStartDtOff + '\'' +
                ", orderEndDt='" + orderEndDt + '\'' +
                ", orderEndDtOff='" + orderEndDtOff + '\'' +
                ", Reserved='" + Reserved + '\'' +
                '}';
    }

    public static Token parseToken(String token){
        if (token.length() != 122){
            throw new IllegalArgumentException("不合法的token长度");
        }
        Token t = new Token();
        t.setType(token.substring(0,1));
        t.setOrderNo(token.substring(1,33));
        t.setUserId(token.substring(33,65));
        t.setCarId(token.substring(65,82));
        t.setOrderStartDt(token.substring(82,96));
        t.setOrderStartDtOff(token.substring(96,100));
        t.setOrderEndDt(token.substring(100,114));
        t.setOrderEndDtOff(token.substring(114,118));
        t.setReserved(token.substring(118,122));
        return t;
    }

    /**
     * 打印保留字段
     */
    public void printReserved(){
//        try {
//            byte[] bytes = Reserved.getBytes("UTF-8");
//            System.out.println("--------打印不可见字符的byte值开始------------");
//            for (byte b : bytes){
//                System.out.println(b);
//            }
//            System.out.println("--------打印不可见字符的byte值结束------------");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
    }
}

